﻿using System;
using System.Linq;
using Coevery.AppFramework.MetadataServiceReference;
using Coevery.Win.DetailViewModule;
using Coevery.AppFramework.WinForms.Extensions;
using Coevery.AppFramework.Extensions;
using Coevery.AppFramework.WinForms.Views;
using Coevery.Win.ProductModule.Detail;
namespace Coevery.Win.ProductModule.Detail
{
    public partial class ProductDetailView : DetailView, IProductDetailView
    {
        public ProductDetailView()
        {
            InitializeComponent();
        }

        public override void InitEditors(Entity entity, Coevery.AppFramework.WinForms.Messages.EntityDetailWorkingMode _workingMode)
        {
            base.InitEditors(entity, _workingMode);
            TransactionCurrencyLookUpEdit.BindLookupList(entity,true);
            ProductTypeCodeLookUpEdit.BindPickList(entity);
            StatusCodeLookUpEdit.BindPickList(entity);
        }
             
        public override void InitValidation()
        {
            SetValidationRule(NameTextEdit, ValidationRules.IsNotBlankRule(ItemForName.Text));
            SetValidationRule(ProductNumberTextEdit, ValidationRules.IsNotBlankRule(ItemForProductNumber.Text));
        }

        private void EntityBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void Currency_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void Currency_InvalidValue(object sender, DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs e)
        {
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
            var ind = this.TransactionCurrencyLookUpEdit.ItemIndex;
            var text = this.TransactionCurrencyLookUpEdit.Text;
            Console.WriteLine(ind + text);

            //var ind = this.lookUpEdit3.ItemIndex;
            if (ind != -1)
            {
                int editvalue = (int)TransactionCurrencyLookUpEdit.Properties.GetDataSourceValue("Value", ind);
                this.TransactionCurrencyLookUpEdit.EditValue = editvalue;
            }
        }

        private void TransactionCurrencyLookUpEdit_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            var old = e.OldValue;
            var newv = e.NewValue;
            Console.WriteLine(old + " " + newv);
        }

        private void StatusCodeLookUpEdit_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void StatusCodeLookUpEdit_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            var old = e.OldValue;
            var newv = e.NewValue;
            Console.WriteLine(old + " " + newv);
        }
    }
}
